/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.trainee.aplikasipenjualan;

import com.trainee.aplikasipenjualan.entity.Barang;
import com.trainee.aplikasipenjualan.entity.PenjualanDetail;
import com.trainee.aplikasipenjualan.entity.JualHeader;
import com.trainee.aplikasipenjualan.service.AplikasipenjualanService;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author BangsJack
 */
public class Main {
    private static ApplicationContext appCtx;
    private static AplikasipenjualanService service;
    private static String kodeBrg;
    private static String namaBrg;
    private static Integer nMenu;
    private static BigDecimal harga;
    private static Integer qty;
    private static String konsumen;
    private static Integer noFaktur;
    private static String date;
    
    
    public static void main(String[] args)  throws IOException, ParseException{
        appCtx = new ClassPathXmlApplicationContext("classpath:com/trainee/aplikasipenjualan/service/impl/applicationContext.xml");
        service = (AplikasipenjualanService) appCtx.getBean("aplikasipenjualanService");

        System.out.println("1. Barang ");
        System.out.println("2. Penjualan ");
        System.out.println("3. Exit");
        System.out.println("Silakan masukkan angka yang anda pilih");
        BufferedReader menu = new BufferedReader(new InputStreamReader(System.in));
        nMenu = Integer.parseInt(menu.readLine());
        if (nMenu == 1) {
            menuBarang();
        }
        else if(nMenu == 2){
            menuPenjualan();
        }
        else{
            System.exit(0);
        }
        
        saveBarang();
        
    }
    
    private static void saveBarang() throws IOException{
            Barang b = new Barang();
            b.setKodeBrg(kodeBrg);
            b.setNamaBrg(namaBrg);
            service.saveBarang(b);
    }
    
    private static void findAll(){
        List<Barang> listB = service.findAllBarang();
        System.out.println("\n-------------------------------");
        System.out.println("| Kode Barang |\t| Nama Barang |");
        System.out.println("-------------------------------");
        for (Barang barang : listB )
        {
            System.out.println("\t"+barang.getKodeBrg()+"\t\t"+barang.getNamaBrg());
        }
    }
    
    private static void delete(Barang b){
        service.deleteBarang(b);
    }
    
    private static void savePenjualan() throws ParseException{
        JualHeader penjualan = new JualHeader();
        PenjualanDetail pd = new PenjualanDetail();
        Barang b = service.findAllBarangById(namaBrg);
        penjualan.setKonsumen(konsumen);
         penjualan.setNoFaktur(noFaktur);
        penjualan.setTglFaktur(new Date());
        pd.setBarang(b);
        pd.setHarga(harga);
        pd.setQuantity(qty);
        pd.setHd(penjualan);
        service.savePenjualan(penjualan);
    }
    
    private static void menuBarang() throws IOException{
        System.out.println("Menu :");
        System.out.println("1. Add Barang");
        System.out.println("2. List all Barang");
        System.out.println("3. Delete Barang");
        System.out.println("4. Exit");
        System.out.println("Silakan masukan angka menu yang anda pilih : ");
        BufferedReader menu = new BufferedReader(new InputStreamReader(System.in));
        nMenu = Integer.parseInt(menu.readLine());
        
        if(nMenu == 1)
        {
            System.out.print("Masukan kode barang: ");
            BufferedReader kode_barang = new BufferedReader(new InputStreamReader(System.in));
            kodeBrg = kode_barang.readLine();
            System.out.print("Masukan nama barang: ");
            BufferedReader nama_barang = new BufferedReader(new InputStreamReader(System.in));
            namaBrg = nama_barang.readLine();
            try {
                saveBarang();
                new Main().menuBarang();
            } catch (Exception e) {
                System.out.println("KODE BARANG YANG DIMASUKKAN TIDAK BOLEH SAMA");
                new Main().menuBarang();
            }
                        
        }
        else if (nMenu == 2) {
            findAll();
            new Main().menuBarang();
        }
        else if (nMenu == 3){
            findAll();
            System.out.print("Masukan kode barang yang ingin di delete: ");
            BufferedReader kode_barang = new BufferedReader(new InputStreamReader(System.in));
            kodeBrg = kode_barang.readLine();
            Barang b = service.findAllBarangById(kodeBrg);
            try {
                delete(b);
                System.out.println("data berhasil di delete\n");
            } catch (Exception e) {
                System.out.println("\nDATA BARANG YANG DIMASUKKAN TIDAK DITEMUKAN!\n");  
            }
            
            new Main().menuBarang();
        }
        else{
            System.exit(0);
        }
    }
    
    private static void menuPenjualan() throws IOException, ParseException{
        
        System.out.print("Masukan nama barang : ");
        BufferedReader nama_barang = new BufferedReader(new InputStreamReader(System.in));
        namaBrg = nama_barang.readLine();
        System.out.print("Masukan harga barang : ");
        BufferedReader hrg = new BufferedReader(new InputStreamReader(System.in));
        harga = BigDecimal.valueOf(Double.valueOf(hrg.readLine()));
        System.out.print("Masukan quantity barang : ");
        BufferedReader qt = new BufferedReader(new InputStreamReader(System.in));
        qty = Integer.parseInt(qt.readLine());
        System.out.print("Masukan nama konsumen : ");
        BufferedReader kons = new BufferedReader(new InputStreamReader(System.in));
        konsumen = kons.readLine();
        System.out.print("Masukan nomor faktur : ");
        BufferedReader faktur = new BufferedReader(new InputStreamReader(System.in));
        noFaktur = Integer.parseInt(faktur.readLine());
        System.out.print("Masukan tanggal faktur: ");
        BufferedReader tgl = new BufferedReader(new InputStreamReader(System.in));
        date = tgl.readLine();
        
        savePenjualan();
        
    }

}
